# load data
file = 'expenditures.txt'
data = read.delim(file, header = TRUE, sep = '\t')
# Print data
head(data)
#import library
library(dplyr)

# group data and determine percentage
data_grp = data %>% 
    group_by(year) %>% 
    mutate(Percent = 100*(expenditure/sum(expenditure)))
# Print data
head(data_grp)
# Load library for plotting data
library(ggplot2)

# Create Plot and custom features
ggplot(data_grp, aes(year, Percent, fill = category)) +
    geom_area() +
    ggtitle("Expenditure Percentages by Category 1984 - 2008",
            subtitle = "The percent of money spent on different categories through the years\nshows how spending habits have changed.") +
    labs(caption = "Source: Data Collected By Nathan Yau",
         x = "",
         y = "") +
    theme_classic() +
    theme(legend.position = "right",
          plot.title = element_text(face = "bold", size = 18),
          plot.subtitle = element_text(color = "light gray"),
          plot.caption = element_text(color = "light gray"),
          axis.title.y = element_text(angle = 0),
          ) +
    scale_fill_manual(values = c("#7D26CD", "#DB70DB", "#990099", "#FF82AB", "#7D26CD", "#DB70DB", "#990099", "#FF82AB", "#7D26CD", "#DB70DB", "#990099", "#FF82AB", "#7D26CD", "#DB70DB")) +
    scale_x_continuous(breaks = seq(1984, 2008, 2)) +
    scale_y_continuous(labels = function(x) paste0(x, "%"))

# load data
file_2 = "unemployement-rate-1948-2010.csv"
data_2 = read.csv(file_2, header = TRUE)
head(data_2)
# load library
library(stringr)

# convert Periods to month names
data_2$Month = month.abb[as.integer(str_remove(data_2$Period, "M"))]

head(data_2)
# group data and determine mean
data_2_grp = data_2 %>% 
    group_by(Month) %>% 
    mutate(Avg_val = mean(Value))
print(data_2_grp)
## # A tibble: 746 x 6
## # Groups:   Month [12]
##    Series.id    Year Period Value Month Avg_val
##    <fct>       <int> <fct>  <dbl> <chr>   <dbl>
##  1 LNS14000000  1948 M01      3.4 Jan      5.67
##  2 LNS14000000  1948 M02      3.8 Feb      5.68
##  3 LNS14000000  1948 M03      4   Mar      5.63
##  4 LNS14000000  1948 M04      3.9 Apr      5.64
##  5 LNS14000000  1948 M05      3.5 May      5.64
##  6 LNS14000000  1948 M06      3.6 Jun      5.67
##  7 LNS14000000  1948 M07      3.6 Jul      5.66
##  8 LNS14000000  1948 M08      3.9 Aug      5.66
##  9 LNS14000000  1948 M09      3.8 Sep      5.66
## 10 LNS14000000  1948 M10      3.7 Oct      5.69
## # … with 736 more rows
# separate data unto unique month and mean values only
data_2_grp_1 = data_2_grp[5:6]
print(unique(data_2_grp_1))
## # A tibble: 12 x 2
## # Groups:   Month [12]
##    Month Avg_val
##    <chr>   <dbl>
##  1 Jan      5.67
##  2 Feb      5.68
##  3 Mar      5.63
##  4 Apr      5.64
##  5 May      5.64
##  6 Jun      5.67
##  7 Jul      5.66
##  8 Aug      5.66
##  9 Sep      5.66
## 10 Oct      5.69
## 11 Nov      5.69
## 12 Dec      5.71
library(treemapify)

# Create Plot with labels and custom features
ggplot(unique(data_2_grp_1), aes(area = Avg_val, fill = Avg_val, label = paste0(as.character(Month), "\n", as.character(round(Avg_val, 2))))) +
  geom_treemap(start = 'topleft', show.legend = FALSE) +
  geom_treemap_text(fontface = "italic", colour = "white", place = "topleft",
                    grow = FALSE, start = 'topleft') + 
  scale_fill_continuous(high = "#132B43", low = "#56B1F7") +
  ggtitle("Average Monthly Unemployment Rates 1948 - 2010",
            subtitle = "Rates vary only slightly with the begining and end of thhe year having higher averages.") +
  labs(caption = "Source: Data Collected By Nathan Yau from Bureau of Labor Statistics") +
  theme(plot.title = element_text(face = "bold", size = 18),
        plot.subtitle = element_text(color = "light gray"),
        plot.caption = element_text(color = "light gray"),
        ) 

# group data and determine average
data_2_grp_year = data_2 %>% 
    group_by(Year) %>% 
    mutate(Avg_val = mean(Value))
# Create Plot selectin year and mean values only and custom features
ggplot(unique(data_2_grp_year[, c(2,6)]), aes(Year, Avg_val)) +
    geom_line(color = "blue") +
    geom_area(fill = "lightblue") +
    ggtitle("Expenditure Percentages by Category 1984 - 2008",
            subtitle = "The percent of money spent on different categories through the years\nshows how spending habits have changed.") +
    labs(caption = "Source: Data Collected By Nathan Yau",
         x = "",
         y = "") +
    theme_classic() +
    theme(legend.position = "right",
          plot.title = element_text(face = "bold", size = 18),
          plot.subtitle = element_text(color = "light gray"),
          plot.caption = element_text(color = "light gray"),
          axis.title.y = element_text(angle = 0),
          ) +
    scale_x_continuous(breaks = seq(1948, 2010, 4))